package com.ailianlian.ablecisi.repository;

import android.content.Context;

import com.ailianlian.ablecisi.baseclass.BaseRepository;
import com.ailianlian.ablecisi.constant.StatusCodeConstant;
import com.ailianlian.ablecisi.pojo.entity.AiCharacter;
import com.ailianlian.ablecisi.pojo.entity.Article;
import com.ailianlian.ablecisi.pojo.entity.Topic;
import com.ailianlian.ablecisi.result.Result;
import com.ailianlian.ablecisi.utils.HttpClient;
import com.ailianlian.ablecisi.utils.JsonUtil;
import com.ailianlian.ablecisi.utils.LoginInfoUtil;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * ailianlian
 * com.ailianlian.ablecisi.repository
 * HomeRepository <br>
 * 数据仓库类，负责处理首页相关的数据请求
 *
 * @author Ablecisi
 * @version 1.0
 * 2025/6/15
 * 星期日
 * 23:20
 */
// HomeRepository.java
public class HomeRepository extends BaseRepository {
    private final Context context;

    public HomeRepository(Context context) {
        super(context);
        this.context = context;
    }

    public void loadHotCharacters(DataCallback<List<AiCharacter>> callback) {
        HttpClient.TOKEN = LoginInfoUtil.getUserToken(getContext());
        getExecutorService().execute(() -> {
            HttpClient.doGet(getContext(), "/character/hot", new HttpClient.HttpCallback() {
                @Override
                public void onSuccess(String response) {
                    Result<List<AiCharacter>> result = JsonUtil.fromJson(response, new TypeToken<Result<List<AiCharacter>>>() {
                    }.getType());
                    if (result != null && result.getCode() == StatusCodeConstant.SUCCESS) {
                        callback.onSuccess(result.getData());
                    } else {
                        callback.onError(result != null ? result.getMsg() : "未知错误");
                    }
                }

                @Override
                public void onFailure(String error) {
                    callback.onError(error);
                }
            });
        });
    }

    // 获取精选内容
    public void loadFeaturedContent(DataCallback<Article> callback) {
        // 获取用户id
        String userId = LoginInfoUtil.getUserId(context);
        System.out.println("当前用户ID: " + userId);
        HttpClient.TOKEN = LoginInfoUtil.getUserToken(getContext());
        getExecutorService().execute(() -> {
            HttpClient.doPost(getContext(), "/article/featured", userId, new HttpClient.HttpCallback() {
                @Override
                public void onSuccess(String response) {
                    Result<Article> result = JsonUtil.fromJson(response, new TypeToken<Result<Article>>() {
                    }.getType());
                    if (result != null && result.getCode() == StatusCodeConstant.SUCCESS) {
                        callback.onSuccess(result.getData());
                    } else {
                        callback.onError(result != null ? result.getMsg() : "未知错误");
                    }
                }

                @Override
                public void onFailure(String error) {
                    callback.onError(error);
                }
            });
        });
    }

    public void loadTopics(DataCallback<List<Topic>> callback) {
        HttpClient.TOKEN = LoginInfoUtil.getUserToken(getContext());
        getExecutorService().execute(() -> {
            HttpClient.doGet(getContext(), "/article/hot", new HttpClient.HttpCallback() {
                @Override
                public void onSuccess(String response) {
                    Result<List<Article>> result = JsonUtil.fromJson(response, new TypeToken<Result<List<Article>>>() {
                    }.getType());
                    if (result != null && result.getCode() == StatusCodeConstant.SUCCESS) {
                        List<Topic> topics = new ArrayList<>();
                        int index = 1; // 用于标识话题的索引
                        for (Article article : result.getData()) {
                            Topic topic = new Topic(article.getId(), article.getTitle(), index, String.valueOf(article.getViewCount()), String.valueOf(article.getComments().size()), String.valueOf(article.getLikeCount()));
                            topics.add(topic);
                            index++;
                        }

                        callback.onSuccess(topics);
                    } else {
                        callback.onError(result != null ? result.getMsg() : "未知错误");
                    }
                }

                @Override
                public void onFailure(String error) {
                    callback.onError(error);
                }
            });
        });
    }

}
